package com.share.main;

import lombok.extern.log4j.Log4j2;
import org.elasticsearch.action.ActionFuture;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.index.query.MatchPhraseQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.FieldSortBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Before;
import org.junit.Test;

import java.net.InetAddress;

/**
 * Created By caifan on 2018/6/20
 */
@Log4j2
public class SearchAPI {
    private TransportClient client ;
    @Before
    public void getClient() throws Exception {
       client = new PreBuiltTransportClient(Settings.EMPTY).addTransportAddress(new TransportAddress(InetAddress.getByName("192.168.5.186"), 9300));
    }
    /**
     * 根据indices types搜索，不指定默认查询所有
     * @throws Exception
     */
    @Test
    public void searchTest_01() throws Exception {
        SearchRequest searchRequest = new SearchRequest("posts");
        searchRequest.types("doc");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.query(QueryBuilders.matchAllQuery());
        ActionFuture<SearchResponse> search = client.search(searchRequest);
        log.info("searchRequest:{}", search.actionGet());
    }

    @Test
    public void searchSourceBuilder() {
        SearchRequest searchRequest = new SearchRequest();
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.termQuery("user", "share"));
        /* sourceBuilder.from(0);
        sourceBuilder.size(5);
        sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
*/
        /**
         * 添加查询条件
         */
        /*QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("user", "share")
                .fuzziness(Fuzziness.AUTO)
                .prefixLength(3)
                .maxExpansions(10);
        sourceBuilder.query(matchQueryBuilder);*/

        /*
         * 查询的多个结果中，如果只有部分包含排序字段，则其他的indices报错 有排序字段的则进行排序
         */
        sourceBuilder.sort(new FieldSortBuilder("age").order(SortOrder.ASC));

        searchRequest.source(sourceBuilder);
        ActionFuture<SearchResponse> search = client.search(searchRequest);
        log.info("searchRequestGet:{}", search.actionGet());
    }

    @Test
    public void multiFieldQueryTest() {
        MatchPhraseQueryBuilder mpq1 = QueryBuilders.matchPhraseQuery("nickname.keyword","六儿00");
        MatchPhraseQueryBuilder mpq2 = QueryBuilders.matchPhraseQuery("username.keyword","赵六0");
        QueryBuilder queryBuilder = QueryBuilders.boolQuery()
                .must(mpq1)
                .must(mpq2);
        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        SearchSourceBuilder query = sourceBuilder.query(queryBuilder);
        sourceBuilder.query(queryBuilder);
        SearchRequest searchRequest = new SearchRequest();
        searchRequest.source(sourceBuilder);
        ActionFuture<SearchResponse> search = client.search(searchRequest);
        log.info("search :{}", search.actionGet());

    }

    @Test
    public void highlightSearchTest() {
        SearchRequest searchRequest = new SearchRequest();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        HighlightBuilder.Field highlightUser = new HighlightBuilder.Field("user");
        highlightUser.highlighterType("unified");
        highlightBuilder.field(highlightUser);
        searchSourceBuilder.highlighter(highlightBuilder);
        searchRequest.source(searchSourceBuilder);
        ActionFuture<SearchResponse> search = client.search(searchRequest);
        log.info("searchRequest:{}", search.actionGet());
    }

}
